//
//  DecorateAppointmentViewController.m
//  customer
//
//  Created by Holothurian on 2018/10/21.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "DecorateAppointmentViewController.h"
#import "CasesOnShowViewController.h"
#import "AppointmentApplyViewController.h"
#import "MaintailIconDetailModel.h"

@interface DecorateAppointmentViewController ()

@property (nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation DecorateAppointmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"装修预约";
    
    [self loadData];
}

- (void)loadData{
    [self showLoading];
    [[QYNetManager sharedManager]POST:kGetMaintailIconDetail parameters:@{@"page":@"0"} andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        [self hideLoading];
        NSLog(@"%@",responseObject[@"data"]);
        if (![responseObject[@"data"] isKindOfClass:[NSNull class]]) {
            [self.dataSource addObjectsFromArray:[NSArray yy_modelArrayWithClass:[MaintailIconDetailModel class] json:responseObject[@"data"][@"object"]]];
            
            [self createMainView];
        }else{
            [self showErrorTipWithTitle:@"获取失败"];
        }
        
    } Failure:^(NSError *error) {
        [self hideLoading];
        [self showErrorTipWithTitle:CustomerPoolNetStr];
    }];
}

- (void)createMainView{
    CGFloat bargin = 20;
    CGFloat leftBarginW = 15;
    CGFloat itemWidth = (F_DEVICE_W - bargin - 2 * leftBarginW)/2.0;//itemView
    CGFloat itemHeight = F_DEVICE_H / 4.0;
    AutoWeakSelf
    for (NSInteger i = 0; i < [self.dataSource count]; i++) {
        MaintailIconDetailModel *model = self.dataSource[i];
        NSInteger line = i / 2;
        NSInteger column = i % 2;
        UIView *itemView = [[UIView alloc]init];
        itemView.layer.cornerRadius = leftBarginW;
        itemView.layer.masksToBounds = YES;
        itemView.tag = i + 100;
        itemView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.view.mas_left).offset(leftBarginW + column * (itemWidth + bargin));
            make.top.equalTo(weakSelf.view).offset(NAVIGATION_BAR_HEIGHT + bargin + line * (itemHeight + bargin));
            make.size.mas_equalTo(CGSizeMake(itemWidth, itemHeight));
        }];
        
        UIImageView *imgView = [[UIImageView alloc]init];
        imgView.layer.cornerRadius = leftBarginW;
        imgView.layer.masksToBounds = YES;
        imgView.userInteractionEnabled = YES;
        [itemView addSubview:imgView];
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.top.equalTo(itemView);
        }];
        [imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kCustomerImageIP,model.icon_img]] placeholderImage:[UIImage imageNamed:@"globalPlaceholder"]];
        
        UIView *grayView = [[UIView alloc]init];
        grayView.backgroundColor = [UIColor lightGrayColor];
        grayView.alpha = 0.7;
        grayView.layer.cornerRadius = leftBarginW;
        grayView.layer.masksToBounds = YES;
        grayView.userInteractionEnabled = YES;
        [itemView addSubview:grayView];
        [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.top.equalTo(itemView);
        }];
        
        UILabel *keyLabel = [[UILabel alloc]init];
        keyLabel.textAlignment = NSTextAlignmentCenter;
        keyLabel.textColor = [UIColor whiteColor];
        keyLabel.font = [UIFont boldSystemFontOfSize:20];
        keyLabel.userInteractionEnabled = YES;
        keyLabel.text = model.icon_name?:@"";
        [itemView addSubview:keyLabel];
        [keyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(itemView);
            make.size.mas_equalTo(CGSizeMake(itemWidth, 30));
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [itemView addGestureRecognizer:tap];
    }
    
}


- (void)tapAction:(UITapGestureRecognizer *)tap{
    NSInteger index = tap.view.tag;
    switch (index) {
        case 100:{
            AppointmentApplyViewController *appoint = [AppointmentApplyViewController new];
            TAB_PUSH(self, appoint,YES);
        }break;
        case 101:{
            CasesOnShowViewController *cases = [CasesOnShowViewController new];
            TAB_PUSH(self, cases, YES);
        }break;
        default: break;
    }
}


- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
